// pages/my/my.js
var util = require('../../utils/util.js');
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: false,     //为true表示openid和userinfo都有，即已登陆
    userInfo:null,
    isHaveResume:null,
    phoneNumber:'',
    loginStatus:true,
    showDialog: true
  },

  onLoad: function () {
    var that = this;
    if (!app.hasLogin()) {
      app.login(function (openId, unionid, sessionkey, userInfo) {
        that.setData({
          userInfo: userInfo,
          isShow: true,
          showDialog: true,
        });
      });
    } else {
      that.setData({
        userInfo: app.getUserInfo(),
        isShow: true,
        showDialog: false,
      });
    }

  
    if (app.globalData.openid != null) {
      var unionid = wx.getStorageSync('unionid');
      util.post(app.api.isHaveResume, { unionid: unionid }, function (res) {
        if (res.data != null) {
          that.setData({
            isHaveResume: res.data
          });
          wx.setStorageSync('isHaveResume', that.data.isHaveResume);
        }
      });
    }
   
  },
 
  onShow: function () {
    //获取我的资料
    var that=this;
    if (app.globalData.openid!=null) {
      that.setData({
        userInfo: that.data.userInfo,
        isShow: true,
        showDialog: true,
      });
    }
    if (app.globalData.isHaveResume == null) {
      var unionid = wx.getStorageSync('unionid');
      util.post(app.api.isHaveResume, { unionid: unionid }, function (res) {
        if (res.data != null) {
          that.setData({
            isHaveResume: res.data
          });
          wx.setStorageSync('isHaveResume', that.data.isHaveResume);
        }
      });
    }
 
  },
  //编辑资料
  editInfoTap: function () {
    wx.navigateTo({
      url: '/pages/edit-my/edit-my',
    })
  },

  bindPhoneTap: function () {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },

  //简历
  resumeTap: function () {
    if (this.data.isShow == true) {
       wx.navigateTo({
         url: '/pages/web-view/web-view',
       })
      
      //新注册入口
      // wx.navigateTo({
      //   url: '/pages/essentialInfo/index',
      // })
    } else {
      app.alert("请先登录!");
    }

  },


  //我的职位
  myDeliveryTap: function () {
   
    if (this.data.isShow == true) {
      wx.navigateTo({
        url: '/pages/my-position/my-position'
      });
    } else {
      app.alert("请先登录!");
    }
   

  },
  //我的预约
  myOrderTap: function () {
    if (this.data.isShow == true){
      wx.navigateTo({
        url: '/pages/my-orders/my-orders'
      });
    } else {
      app.alert("请先登录!");
    }
  },
//我的评价
  myAssessTap:function() {
    if (this.data.isShow == true){
      wx.navigateTo({
        url: '/pages/my-assess/my-assess?nickname'
      });
    } else {
      app.alert("请先登录!");
    }
  },

//交易明细
  payDetailTap: function () {
    if (this.data.isShow == true) {
      wx.navigateTo({
        url: '/pages/my-paydetail/my-paydetail'
      });
    } else {
     app.alert("请先登录!");
     
    }

  },
//发布职位/顾问入驻
  createjobTap: function (event) {
    wx.navigateTo({
      url: '../service/service'
    });
  },

//我的反馈与投诉
  knowTap: function (event) {
    wx.navigateTo({
      url: '../opinion/opinion'
    });
  },
  //快速登陆
  getPhoneNumber: function (e) {
    var that=this;
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
    var sessionkey=wx.getStorageSync('sessionkey');
    util.post(app.api.getPhoneNumber, { openid: app.globalData.openid, iv: e.detail.iv, encrydata: e.detail.encryptedData, sessionKey:sessionkey}, function (res) {
      if (res.data != null) {
        that.setData({
          phoneNumber: res.data.phoneNumber
        });
        console.log(that.data.phoneNumber);
        wx.setStorageSync('phone', that.data.phoneNumber);
      }
    });
  },
  //编辑头像
  imgTap: function () {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths

        app.globalData.userInfo.avatarUrl = tempFilePaths;
        wx.setStorageSync('userInfo', app.globalData.userInfo);
        that.setData({
          userInfo: app.globalData.userInfo
        })

      }
    })
  },
  login: function () {
    var that = this;
    app.login(function (openId, unionid, userInfo, wxcode) {
      that.setData({
        userInfo: userInfo,
        isShow: true
      });
    });
  },
  // 退出账户登录
  signOut: function () {
    var _this = this;

    //   恢复到没登录的样式
    try {
      wx.setStorageSync('token', 'false');
    } catch (e) {
    }
    try {
      wx.removeStorageSync('openid')
    } catch (e) {
      // Do something when catch error
    }
    if (wx.getStorageSync('token') == "false" && wx.getStorageSync('openid') == "") {
      wx.showModal({
        title: '提示',
        content: '退出登录成功',
        success: function (res) {
          if (res.confirm) {
            _this.setData({
              user: false,
              usersetting: true
            });
            app.globalData.token = false;

          } else if (res.cancel) {
            // console.log('用户点击取消')
          }
        }
      })
    }
  },
  //关闭模态窗getPhoneNumber
  clearDialog: function () {
    this.setData({
      showDialog: false
    });
  },
  //微信账号快速登陆
  getPhoneNumber: function (e) {
    var that = this;
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
    var sessionkey = wx.getStorageSync('sessionkey');
    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '未授权',
        success: function (res) { }
      })
    } else {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '同意授权',
        success: function (res) {
          util.post(app.api.getPhoneNumber, { openid: app.globalData.openid, iv: e.detail.iv, encrydata: e.detail.encryptedData, sessionKey: sessionkey }, function (res) {
            if (res.data != null) {
              that.setData({
                phoneNumber: res.data.phoneNumber
              });
              // 授权已提交
              wx.showToast({
                title: '登录成功',
                icon: 'success',
                duration: 2000
              })
              that.clearDialog();
              console.log(that.data.phoneNumber);
              wx.setStorageSync('phone', that.data.phoneNumber);
            }
          });

        }
      })
    }
  },

})